using System;
using System.Collections;
using NodeCanvas.DialogueTrees;
using UnityEngine;

public class StoryActor : StoryActorBase
{
	public UILabel text;

	public new UILabel name;

	public UISprite actorSprite;

	public UISprite textBackground;

	public UIWidget continueWidget;

	private DialogueSpeechInfo speech;

	private bool speechDone;

	private bool actorShown;

	public override void Hide()
	{
		StopAllCoroutines();
		base.actorObject.SetActive(false);
		speech = null;
	}

	public override void OnActorSpeaking(DialogueSpeechInfo speech, bool changeActor)
	{
		Init();
		speechDone = false;
		actorShown = !changeActor;
		this.speech = speech;
		base.actorObject.SetActive(true);
		continueWidget.cachedGameObject.SetActive(false);
		StopAllCoroutines();
		text.text = string.Empty;
		DialogueActor actor = speech.actor;
		string empty = actor.name;
		Color dialogueColor = actor.dialogueColor;
		GGActorSetup component = actor.GetComponent<GGActorSetup>();
		if (component != null && component.hideName)
		{
			empty = string.Empty;
		}
		if (actorSprite != null)
		{
			Debug.Log(string.Concat("Setup ", component, " sprite name ", component.spriteName, " Name ", component.gameObject.name));
			if (component != null && !string.IsNullOrEmpty(component.spriteName))
			{
				actorSprite.spriteName = component.spriteName;
				actorSprite.gameObject.SetActive(true);
			}
			else
			{
				actorSprite.gameObject.SetActive(false);
			}
		}
		UITools.ChangeText(name, empty);
		textBackground.color = dialogueColor;
		if (changeActor)
		{
			StartCoroutine(ShowActor(empty, dialogueColor, delegate
			{
				StartCoroutine(ProcessStatement(speech.statement, SpeechDone));
			}));
		}
		else
		{
			StartCoroutine(ProcessStatement(speech.statement, SpeechDone));
		}
		textBackground.color = Color.white;
		textBackground.spriteName = "dialog-bck-down";
		text.color = Color.black;
	}

	public void SpeechDone()
	{
		speechDone = true;
		continueWidget.cachedGameObject.SetActive(true);
		continueWidget.alpha = 0f;
		TweenAlpha.Begin(continueWidget.cachedGameObject, 0.1f, 1f);
	}

	public IEnumerator ShowActor(string actorName, Color color, Action callback)
	{
		Transform transform = base.actorObject.transform;
		transform.localScale = new Vector3(1f, 0f, 1f);
		float scaleDuration = 0.25f;
		float delay = 0f;
		base.actorObject.SetActive(true);
		TweenScale.Begin(base.actorObject, scaleDuration, Vector3.one);
		yield return new WaitForSeconds(scaleDuration + delay);
		callback();
	}

	public IEnumerator ProcessStatement(Statement statement, Action callback)
	{
		actorShown = true;
		float durationBetweenLetters = 0.05f;
		float durationInterpunction = 0.5f;
		float durationComma = 0.1f;
		float endDuration = 0.25f;
		float initialDelay = 0.25f;
		string speech = string.Empty;
		yield return new WaitForSeconds(initialDelay);
		for (int i = 0; i < statement.text.Length; i++)
		{
			speech += statement.text[i];
			text.text = speech;
			yield return new WaitForSeconds(durationBetweenLetters);
			char c = statement.text[i];
			if (c == '.' || c == '!' || c == '?')
			{
				yield return new WaitForSeconds(durationInterpunction);
			}
			if (c == ',')
			{
				yield return new WaitForSeconds(durationComma);
			}
		}
		yield return new WaitForSeconds(endDuration);
		callback();
	}

	public override void OnClick()
	{
		if (actorShown && speech != null)
		{
			if (speechDone)
			{
				DialogueSpeechInfo dialogueSpeechInfo = speech;
				speech = null;
				dialogueSpeechInfo.Continue();
			}
			else
			{
				StopAllCoroutines();
				text.text = speech.statement.text;
				SpeechDone();
			}
		}
	}
}
